package com.lixin.admin.server.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lixin.admin.server.convert.RoleConvert;
import com.lixin.admin.server.convert.RoleCreateConvert;
import com.lixin.admin.server.convert.RoleUpdateConvert;
import com.lixin.admin.server.dao.RoleMapper;
import com.lixin.admin.server.model.Role;
import com.lixin.admin.server.service.RoleService;
import com.lixin.admin.server.vo.RoleCreateVO;
import com.lixin.admin.server.vo.RoleDelVO;
import com.lixin.admin.server.vo.RoleUpdateVO;
import com.lixin.admin.server.vo.RoleVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Override
    public RoleVO get(Long id) {
        Role role = getById(id);
        return RoleConvert.INSTANCE.entity2vo(role);
    }

    @Override
    public List<RoleVO> getList(Long page, Long pageSize) {
        IPage<Role> roleIPage = new Page<>(page, pageSize);
        IPage<Role> rolePage = page(roleIPage);
        List<Role>  roleList= rolePage.getRecords();
        return RoleConvert.INSTANCE.entityList2voList(roleList);
    }

    @Override
    public boolean create(RoleCreateVO roleCreateVO) {
        Role role = RoleCreateConvert.INSTANCE.vo2entity(roleCreateVO);
        role.setRoleCreated(LocalDateTime.now());
        role.setRoleUpdated(LocalDateTime.now());
        return save(role);
    }

    @Override
    public void create(Role role) {
        role.setRoleCreated(LocalDateTime.now());
        role.setRoleUpdated(LocalDateTime.now());
        this.baseMapper.addRole(role);
    }

    @Override
    public boolean modify(RoleUpdateVO roleUpdateVO) {
        Role role = RoleUpdateConvert.INSTANCE.vo2entity(roleUpdateVO);
        role.setRoleUpdated(LocalDateTime.now());
        return updateById(role);
    }

    @Override
    public boolean del(RoleDelVO roleDelVO) {
        return removeByIds(roleDelVO.getIds());
    }
}
